package fu.capstone.hanoicityguide.dataaccess.tour;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import fu.capstone.hanoicityguide.business.model.Tour;
import fu.capstone.hanoicityguide.dataaccess.base.BaseDAOImpl;
import fu.capstone.hanoicityguide.dataaccess.base.DatabaseHelper;

public class TourDAOImp extends BaseDAOImpl implements TourDAO {

	private SQLiteDatabase database;
	private DatabaseHelper dbHelper;
	private Context context;

	public TourDAOImp(Context ctx) {
		super(ctx);
		this.context = ctx;
	}

	@Override
	public Tour browseTour(int tourId) {

		// <Id>2</Id>
		// <Name>a</Name>
		// <ProviderId>1</ProviderId>
		// <RatingId>1</RatingId>
		// <AlbumImageId>3</AlbumImageId>
		// <Price>1</Price>
		// <Duration>1</Duration>
		// <Description>a</Description>
		// <DBVersionId>1</DBVersionId>

		Cursor cursor = null;
		Tour tour = new Tour();
		ArrayList<String> arrImage = new ArrayList<String>();
		try {
			dbHelper = connectDB();
			database = dbHelper.getReadableDatabase();
			String queryListTour = "SELECT Tour.[Id], "
					+ "Tour.[Name], "
					+ "Tour.[Description], "
					+ "Tour.[Duration], "
					+ "Tour.[Price], "
					+ "TourProvider.[Name] AS 'ProviderName', TourProvider.[PhoneNumber] , "
					+ "AlbumImage.[ImagePath1], "
					+ "AlbumImage.[ImagePath2], "
					+ "AlbumImage.[ImagePath3], "
					+ "AlbumImage.[ImagePath4], "
					+ "AlbumImage.[ImagePath5] FROM Tour "
					+ "LEFT JOIN AlbumImage ON Tour.[AlbumImageId] = AlbumImage.[Id] "
					+ "LEFT JOIN TourProvider ON Tour.[ProviderId] = TourProvider.[Id] "
					+ "WHERE Tour.[Id] == " + tourId;

			cursor = database.rawQuery(queryListTour, null);
			if (cursor != null) {
				if (cursor.moveToFirst()) {
					do {

						tour = new Tour();
						tour.setId(cursor.getInt(cursor.getColumnIndex("Id")));
						tour.setName(cursor.getString(cursor
								.getColumnIndex("Name")));
						tour.setDescription(cursor.getString(cursor
								.getColumnIndex("Description")));
						tour.setPrice(Float.parseFloat(cursor.getString(cursor
								.getColumnIndex("Price"))));
						tour.setProviderName(cursor.getString(cursor
								.getColumnIndex("ProviderName")));
						tour.setDuration(Float.parseFloat(cursor
								.getString(cursor.getColumnIndex("Duration"))));
						tour.setPhoneNumber(cursor.getString(cursor
								.getColumnIndex("PhoneNumber")));
						arrImage.add(cursor.getString(cursor
								.getColumnIndex("ImagePath1")));
						arrImage.add(cursor.getString(cursor
								.getColumnIndex("ImagePath2")));
						arrImage.add(cursor.getString(cursor
								.getColumnIndex("ImagePath3")));
						arrImage.add(cursor.getString(cursor
								.getColumnIndex("ImagePath4")));
						arrImage.add(cursor.getString(cursor
								.getColumnIndex("ImagePath5")));

						tour.setArrImage(arrImage);

					} while (cursor.moveToNext());
				}
			}
		} catch (Exception e) {
			Log.v("Can't query DB browseTour : ", e.toString());
		} finally {
			closeDB(database, dbHelper);
		}
		return tour;
	}

}
